<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #result {
            width: 200px;
            height: 180px;
            border: 1px solid brown;
        }
    </style>
</head>
<body>
    <div id="result"></div>
    <script>
        const result = document.getElementById('result');
        window.onkeydown = function() {
            
            const add = new XMLHttpRequest();
            add.responseType = 'json';
            add.open('GET', 'http://127.0.0.1:8000/json-server')
            add.send();
            add.onreadystatechange = function() {
                if(add.readyState === 4) {
                    if(add.status >= 200 && add.status < 300) {
                        // result.innerHTML = add.response;
                        // console.log(add.response);
                        //手动对数据转换
                        // let data = JSON.parse(add.response);
                        // console.log(data);
                        // result.innerHTML = data.name;
                        //自动转换
                        // add.responseType = 'json';
                        result.innerHTML = add.response.name;

                    }
                }
            }
        }
    </script>
</body>
</html>